﻿/*Copyright (C) 2010-2012 RAAFCOM B.V. All rights reserved.

This file is part of Velsto and may be used under the terms of:
The GNU General Public License version 3 or higher, as published by
the Free Software Foundation.

This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.*/
/* 
* Application  : Velsto
* Class        : InpFile
* 04/06/2010   : M.A.van Ravesteijn (Raafcom)
*                Initial version
*                
* 
* Class with method to interface SimulationEntity objects with Velsim via
* .INP file
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Globalization;

namespace Velsto
{
    public class InpFile
    {
        public InpFile()
        {
        }

        // Method to create .INP file (file with parameters for Dust).
        public string saveInpfile(ref SimulationEntity se, string inpFilename)
        {
            try
            {
                TextWriter inpFile = new StreamWriter(inpFilename);
                inpFile.WriteLine(se.ProjectName);
                // for Single point simulations use selected point
                if (se.SimulOption == Constants.SIM_SINGLE_POINT_TD
                 || se.SimulOption == Constants.SIM_SINGLE_POINT)
                {
                    inpFile.WriteLine(se.JoinSingleFile);
                }
                else
                {
                    inpFile.WriteLine(se.JoinInputFile);
                }
                // write inputfiles while creating index for grids in joined file
                int i2 = 0;
                int[] iv0 = new int[10];
                int[] ivvndev = new int[10];
                int[] itwtdev = new int[10];
                for (int i = 0; i < 10; i++)
                {
                    if (((String)se.AlInputFileTWT[i]).Length == 0)
                    {
                        inpFile.WriteLine("a");
                    }
                    else
                    {
                        inpFile.WriteLine((String)se.AlInputFileTWT[i]);
                    }
                }
                for (int i = 0; i < 10; i++)
                {
                    if (((String)se.AlInputFileTWTdev[i]).Length == 0)
                    {
                        inpFile.WriteLine("a");
                        itwtdev[i] = -1;
                    }
                    else
                    {
                        inpFile.WriteLine((String)se.AlInputFileTWTdev[i]);
                        itwtdev[i] = i2;
                        i2++;
                    }
                }
                for (int i = 0; i < 10; i++)
                {
                    if (((String)se.AlInputFileV0[i]).Length == 0)
                    {
                        inpFile.WriteLine("a");
                        iv0[i] = -1;
                    }
                    else
                    {
                        inpFile.WriteLine((String)se.AlInputFileV0[i]);
                        iv0[i] = i2;
                        i2++; 
                    }
                }
                for (int i = 0; i < 10; i++)
                {
                    if (((String)se.AlInputFileF[i]).Length == 0)
                    {
                        inpFile.WriteLine("a");
                    }
                    else
                    {
                        inpFile.WriteLine((String)se.AlInputFileF[i]);
                    }
                }
                for (int i = 0; i < 10; i++)
                {
                    if (((String)se.AlInputFileVVNdev[i]).Length == 0)
                    {
                        inpFile.WriteLine("a");
                        ivvndev[i] = -1;
                    }
                    else
                    {
                        inpFile.WriteLine((String)se.AlInputFileVVNdev[i]);
                        ivvndev[i] = i2;
                        i2++;
                    }
                }
                
                int nrgrid = i2;
                // write grid array indexes
                inpFile.WriteLine(nrgrid.ToString());
                for (int i = 0; i < 10; i++)
                {
                    inpFile.WriteLine(itwtdev[i].ToString());
                }
                for (int i = 0; i < 10; i++)
                {
                    inpFile.WriteLine(iv0[i].ToString());
                }
                for (int i = 0; i < 10; i++)
                {
                    inpFile.WriteLine(ivvndev[i].ToString());
                }
                // write outputfiles different ones for Single point simulations
                if (se.SimulOption == Constants.SIM_SINGLE_POINT_TD
                 || se.SimulOption == Constants.SIM_SINGLE_POINT)
                {
                    se.SingleAvzFile = se.ResultFolderpath + "\\" + se.ProjectName + "_SINGLE.AVZ";
                    inpFile.WriteLine(se.SingleAvzFile);
                    se.SingleSdzFile = se.ResultFolderpath + "\\" + se.ProjectName + "_SINGLE.SDZ";
                    inpFile.WriteLine(se.SingleSdzFile);
                    se.SingleRepFile = se.ResultFolderpath + "\\" + se.ProjectName + "_SINGLE.REP";
                    inpFile.WriteLine(se.SingleRepFile);
                }
                else
                {
                    se.AvzFile = se.ResultFolderpath + "\\" + se.ProjectName + ".AVZ";
                    inpFile.WriteLine(se.AvzFile);
                    se.SdzFile = se.ResultFolderpath + "\\" + se.ProjectName + ".SDZ";
                    inpFile.WriteLine(se.SdzFile);
                    se.RepFile = se.ResultFolderpath + "\\" + se.ProjectName + ".REP";
                    inpFile.WriteLine(se.RepFile);
                }
                // write nrlayers, simulation option and layer parameters
                inpFile.WriteLine(se.NrLayers.ToString());
                inpFile.WriteLine(se.SimulOption.ToString());
                inpFile.WriteLine(se.SimulNr.ToString());
                for (int i = 0; i < se.NrLayers; i++)
                {
                    inpFile.WriteLine(
                    (i+1).ToString() + " " +
                    Convert.ToString(((LayerparamsEntity)(se.AlLayerParams[i])).V0vintOption) + " " +
                    ((LayerparamsEntity)(se.AlLayerParams[i])).V0vint.ToString("0.0",CultureInfo.InvariantCulture) + " " +
                    ((LayerparamsEntity)(se.AlLayerParams[i])).V0vintDev.ToString("0.0", CultureInfo.InvariantCulture) + " " +
                    ((LayerparamsEntity)(se.AlLayerParams[i])).K.ToString("0.0000", CultureInfo.InvariantCulture) + " " +
                    ((LayerparamsEntity)(se.AlLayerParams[i])).KDev.ToString("0.0000", CultureInfo.InvariantCulture) + " " +
                    ((LayerparamsEntity)(se.AlLayerParams[i])).F.ToString("0.0000", CultureInfo.InvariantCulture) + " " +
                    Convert.ToString(((LayerparamsEntity)(se.AlLayerParams[i])).DepV0K) + " " +
                    ((LayerparamsEntity)(se.AlLayerParams[i])).TwtDev.ToString("0.0", CultureInfo.InvariantCulture) + " " +
                    Convert.ToString(((LayerparamsEntity)(se.AlLayerParams[i])).DepVel)
                    );
                }
                inpFile.Close();
                return null;
            }
            catch (Exception ex)
            {
                return ex.ToString();
            }
        }

        // Method to create .INP file (file with parameters for Velsim).
        public string saveInpfile_Velsim(ref SimulationEntity se, string inpFilename)
        {
            try
            {
                TextWriter inpFile = new StreamWriter(inpFilename);
                inpFile.WriteLine(se.ProjectName);
                // for Single point simulations use selected point
                if (se.SimulOption == Constants.SIM_SINGLE_POINT_TD
                 || se.SimulOption == Constants.SIM_SINGLE_POINT)
                {
                    inpFile.WriteLine(se.JoinSingleFile);
                }
                else
                {
                    inpFile.WriteLine(se.JoinInputFile);
                }
                // write inputfiles while creating index for grids in joined file
                int i2 = 0;
                int[] iv0 = new int[10];
                int[] ivvndev = new int[10];
                int[] itwtdev = new int[10];
                for (int i = 0; i < 10; i++)
                {
                    if (((String)se.AlInputFileTWTdev[i]).Length == 0)
                    {
                        itwtdev[i] = -1;
                    }
                    else
                    {
                        itwtdev[i] = i2;
                        i2++;
                    }
                }
    
                for (int i = 0; i < 10; i++)
                {
                    if (((String)se.AlInputFileV0[i]).Length == 0)
                    {
                        iv0[i] = -1;
                    }
                    else
                    {
                        iv0[i] = i2;
                        i2++;
                    }
                }
    
                for (int i = 0; i < 10; i++)
                {
                    if (((String)se.AlInputFileVVNdev[i]).Length == 0)
                    {
                        ivvndev[i] = -1;
                    }
                    else
                    {
                        ivvndev[i] = i2;
                        i2++;
                    }
                }

                int nrgrid = i2;
                // write grid array indexes
                //inpFile.WriteLine(nrgrid.ToString());
                for (int i = 0; i < 10; i++)
                {
                    inpFile.WriteLine(itwtdev[i].ToString());
                }
                for (int i = 0; i < 10; i++)
                {
                    inpFile.WriteLine(iv0[i].ToString());
                }
                for (int i = 0; i < 10; i++)
                {
                    inpFile.WriteLine(ivvndev[i].ToString());
                }
                // write outputfiles different ones for Single point simulations
                if (se.SimulOption == Constants.SIM_SINGLE_POINT_TD
                 || se.SimulOption == Constants.SIM_SINGLE_POINT)
                {
                    se.SingleAvzFile = se.ResultFolderpath + "\\" + se.ProjectName + "_SINGLE.AVZ";
                    inpFile.WriteLine(se.SingleAvzFile);
                    se.SingleSdzFile = se.ResultFolderpath + "\\" + se.ProjectName + "_SINGLE.SDZ";
                    inpFile.WriteLine(se.SingleSdzFile);
                    se.SingleRepFile = se.ResultFolderpath + "\\" + se.ProjectName + "_SINGLE.REP";
                    inpFile.WriteLine(se.SingleRepFile);
                }
                else
                {
                    se.AvzFile = se.ResultFolderpath + "\\" + se.ProjectName + ".AVZ";
                    inpFile.WriteLine(se.AvzFile);
                    se.SdzFile = se.ResultFolderpath + "\\" + se.ProjectName + ".SDZ";
                    inpFile.WriteLine(se.SdzFile);
                    se.RepFile = se.ResultFolderpath + "\\" + se.ProjectName + ".REP";
                    inpFile.WriteLine(se.RepFile);
                }
                // write nrlayers, simulation option and layer parameters
                inpFile.WriteLine(se.NrLayers.ToString());
                inpFile.WriteLine(se.SimulOption.ToString());
                inpFile.WriteLine(se.SimulNr.ToString());
                for (int i = 0; i < se.NrLayers; i++)
                {
                    inpFile.WriteLine(
                    (i + 1).ToString() + " " +
                    Convert.ToString(((LayerparamsEntity)(se.AlLayerParams[i])).V0vintOption) + " " +
                    ((LayerparamsEntity)(se.AlLayerParams[i])).V0vint.ToString("0.0", CultureInfo.InvariantCulture) + " " +
                    ((LayerparamsEntity)(se.AlLayerParams[i])).V0vintDev.ToString("0.0", CultureInfo.InvariantCulture) + " " +
                    ((LayerparamsEntity)(se.AlLayerParams[i])).K.ToString("0.0000", CultureInfo.InvariantCulture) + " " +
                    ((LayerparamsEntity)(se.AlLayerParams[i])).TwtDev.ToString("0.0", CultureInfo.InvariantCulture)
                    );
                }
                inpFile.Close();
                return null;
            }
            catch (Exception ex)
            {
                return ex.ToString();
            }
        }
    }
}
